package tss.server;

import java.net.SocketAddress;
import java.nio.channels.SocketChannel;

import tss.common.ExtensionStates;

	class Extension
	{
		private SocketAddress address = null;
		private ExtensionStates extensionState = ExtensionStates.Unavailable;
		private int extensionNumber = -1;
		private SocketChannel channel = null;
		private RouteDetail route = null;
		
		public Extension(int extensionNumber){

			this.extensionNumber = extensionNumber;
		}
		
		public int getExtensionNumber() { return this.extensionNumber; }

		public SocketAddress getAddress() { return this.address; }
		public SocketChannel getChannel() { return this.channel; }
		
		public ExtensionStates getExtensionState() { return this.extensionState; }
		
		public RouteDetail getRoute() { return this.route; }

		public void setBusy(RouteDetail route) {
			if (route != null) {
				this.route = route;
				this.extensionState = ExtensionStates.Busy;
			}
		}
		
		public void setAvailable(SocketChannel channel) {
			if (channel != null) {
				this.channel = channel;
				this.address = channel.socket().getRemoteSocketAddress();
				this.extensionState = ExtensionStates.Available;
			} else {
				this.extensionState = ExtensionStates.Unavailable;
			}
		}
		
		public void setAvailableDisconnect() {
			if (this.route != null) {
				this.route = null;
				this.extensionState = ExtensionStates.Available;
			}
		}
		
		public void setUnavailable() {
			this.address = null;
			this.channel = null;
			this.extensionState = ExtensionStates.Unavailable;
		}
	}
